var data = require('../../data/local')
var app = getApp();
Page({

   data: {
      isCollected: false,
      item: "",
      isPlay: false,
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
      var { id } = options;
      var { postList } = data;
      this.setData({
         item: postList[id],
         id
      })
      /* 有缓存取得缓存,没有缓存设计缓存
      缓存的数据结构
 
      {
        "0":"false",
        "1":"true",
        "2":"false",
        "3":"true"
      }
       */
      var collection = wx.getStorageSync('collection');
      /* 有缓存,获取缓存 */
      if (collection) {
         let collected = collection[id];
         this.setData({
            isCollected: collected
         })
      } else {
         /*没有缓存,就设置缓存 */
         var collection = {};
         collection[id] = false;
         wx.setStorageSync('collection', collection)
         /* {"0":"false","1":"false"} */
      };
      wx.onBackgroundAudioPause(()=> {
            this.setData({
               isPlay:false
            })
      });
      wx.onBackgroundAudioPlay(()=> {
         this.setData({
            isPlay:true
         })
      });
      if(app.globalData.g_isPlay && app.globalData.g_playId==this.data.item.postId){
         this.setData({
            isPlay:true
         });
      }

   },
   handleCollect() {
      /* 获取缓存 */
      var collection = wx.getStorageSync('collection')
      var collected = !collection[this.data.id];
      collection[this.data.id] = collected;
      wx.setStorageSync('collection', collection);

      if(!this.data.isCollected){
         wx.showModal({
            content: '收藏文章',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#000000',
            confirmText: '确定',
            confirmColor: '#3CC51F',
            success: (res) => {
               if (res.confirm) {
                  this.setData({
                     isCollected: collected
                  })
               }
            }
         });
      }else{
         wx.showModal({
            content: '确定取消收藏',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#000000',
            confirmText: '确定',
            confirmColor: '#3CC51F',
            success: (res) => {
               if (res.confirm) {
                  this.setData({
                     isCollected: collected
                  })
               }
            }
         });
      }

   },
   handleShare(){
      wx.showActionSheet({
         itemList: [
            "分享微信",
            "分享朋友圈"
         ],
         itemColor: '#000000',
         success: (result)=>{
            
         }
      });
   },
   play() {
      if (this.data.isPlay) {
         //2.暂停
         wx.pauseBackgroundAudio();
         this.setData({
            isPlay: false
         })
      } else {
         //1.开始播放
         wx.playBackgroundAudio({
            dataUrl: this.data.item.music.url,
            title: this.data.item.music.title
         });
         this.setData({
            isPlay: true
         })
         app.globalData.g_playId = this.data.item.postId;
         app.globalData.g_isPlay = true;
      }
   },
   
})